<?php

namespace app\queue\stomp;

use app\queue\BaseConsumer;
use common\model\ChatModel;
use common\model\UserModel;
use common\service\MessageService;

class SendSystemHitMessageConsumer extends BaseConsumer
{

    // 要消费的队列名
    public $queue = 'send_system_hit_message';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * @param $data
     */
    public function doConsume($data)
    {
        try {
            $chat = model(ChatModel::class)->where(['global_id' => $data['chat_global_id']])->find();
            if (empty($chat)) {
                return;
            }
            $chat['chat_global_id'] = $chat['global_id'];
            if (!empty($data['user_id_list'])) {
                $userList = model(UserModel::class)->where(['id' => ['IN', $data['user_id_list']]])->select();
            } else {
                $userList = [];
            }
            $isUpdate = $data['is_update'] ?? true;
            MessageService::getInstance()->pushSystemHitMessage($data['message'], $userList, $chat, $isUpdate);
        } catch (\Throwable $e) {
            trace(format_exception_info($e), 'ERR');
        }
    }
}
